package com.meta.act.app.domain.tenant;

import com.meta.framework.common.utils.SecurityUtils;
import com.meta.framework.exception.PlatformErrorCode;
import com.meta.act.app.tenant.common.FileTypeEnum;
import com.meta.act.app.tenant.entity.TenantBase;
import com.meta.act.app.tenant.req.TenantCreateCmd;
import com.meta.act.app.tenant.req.TenantEditCmd;
import lombok.Data;
import org.junit.Assert;

import java.util.List;
import java.util.stream.Collectors;

/*/**
* @Description:
* @Param:
* @return:
* @Author: simon.Zhang
* @Date: 2020/11/5
*/
@Data
//@Builder
public class TenantDo extends TenantBase {

	private TenantExtDo tenantExtDo;
	private List<TenantFileDo> creditFileList;
	private List<TenantFileDo> taxFileList;

	/*/**
	* @Description:
	* @Param: [cmd, labelId] 
	* @return: com.metafnt.tenant.domain.tenant.TenantDo
	* @Author: simon.Zhang 
	* @Date: 2020/11/5 
	*/ 
	public static TenantDo of(TenantCreateCmd cmd, Long tenantId){
		Assert.assertNotNull(PlatformErrorCode.PARAM_NOT_NULL.getErrorMsg(), cmd.getName());
		Assert.assertNotNull(PlatformErrorCode.PARAM_NOT_NULL.getErrorMsg(), cmd.getFullName());

		TenantDo tenantDo = cmd.transform(TenantDo.class);
		tenantDo.setCreditFileList(TenantFileDo.of(cmd,tenantId, FileTypeEnum.CREDIT_FILE.getCode()));
		tenantDo.setTaxFileList(TenantFileDo.of(cmd,tenantId, FileTypeEnum.TAX_FILE.getCode()));
		tenantDo.setTenantExtDo(TenantExtDo.of(cmd,tenantId));
		tenantDo.setCreateUserId(SecurityUtils.getUserId());
		return tenantDo;
	}

	public static TenantDo of(TenantCreateCmd cmd){
		Assert.assertNotNull(PlatformErrorCode.PARAM_NOT_NULL.getErrorMsg(), cmd.getName());
		Assert.assertNotNull(PlatformErrorCode.PARAM_NOT_NULL.getErrorMsg(), cmd.getFullName());
		TenantDo tenantDo = cmd.transform(TenantDo.class);
		tenantDo.setCreateUserId(SecurityUtils.getUserId());
		return tenantDo;
	}

	public void edit(TenantEditCmd cmd){
		cmd.transform(this);
		this.setTenantExtDo(cmd.transform(TenantExtDo.class));
		this.setCreditFileList(cmd.getCreditFiles().stream().map(x -> x.transform(TenantFileDo.class)).collect(Collectors.toList()));
		this.setTaxFileList(cmd.getTaxFiles().stream().map(x -> x.transform(TenantFileDo.class)).collect(Collectors.toList()));
	}
}
